//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Base/Utils/Time/TimeProfiler.h"

namespace Engine
{
namespace Base
{
namespace Profiler
{

	//
	// Function Profiler
	//

	struct FunctionProfiler
	{
	// variables
	private:
		TimeProfiler<uint64>	_time;
		string					_name;
		float					_limit;		// normal time for function, if real time is greather then log message marked as warning


	// methods
	public:
		explicit
		FunctionProfiler (uni_c_string name, float limit = 1.0e30f) : _name(name), _limit(limit)
		{
			_time.Start();
		}

		FunctionProfiler (uni_c_string name, uni_c_string func, uni_c_string file, int line, float limit = 1.0e30f) : _limit(limit)
		{
			_name << "profiler: " << name << ", function: " << func << ", file: " << file << ", line: " << line;
			_time.Start();
		}

		~FunctionProfiler ()
		{
			const float	dt = (float)_time.GetTimeDelta().GetNanoSec();
			
			string s("time delta: ");

			StringUtils::TimeToStr( s, dt );
			s << ", ";
			_name >> s;

			LOG( _name.cstr(), dt < _limit ? ELog::DEBUG : ELog::WARN );
		}
	};


# ifdef OP_ENABLE_PROFILING

#	define FUNC_PROFILER( _name )					FUNC_PROFILER_L( _name, 1.0e30f )

#	define FUNC_PROFILER_L( _name, _limit ) \
		Engine::Base::Profiler::FunctionProfiler	__funcProf##_name( #_name, UX_FUNCTION_NAME, __FILE__, __LINE__, _limit )

# else

#	define FUNC_PROFILER( _name )
#	define FUNC_PROFILER_L( _name, _limit )

# endif	// OP_ENABLE_PROFILING


}	// Profiler
}	// Base
}	// Engine